package com.ate.adapters;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.ate.bean.BuyCarBean;
import com.ate.bean.GoodsBean;
import com.ate.gen.DaoManager;
import com.ate.gen.DaoSession;
import com.ate.javamvp.R;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;


/**
 * @author Ate
 * @description:
 * @date :2020/12/25 10:48
 */
public class BuyCarAdapter extends BaseQuickAdapter<BuyCarBean, BaseViewHolder> implements View.OnClickListener {
    private OnAumentListener onAumentListener;

    public void setOnAumentListener(OnAumentListener onAumentListener) {
        this.onAumentListener = onAumentListener;
    }

    public BuyCarAdapter(int layoutResId, @Nullable List<BuyCarBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BuyCarBean item) {
        Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.item_img));
        helper.setText(R.id.item_name, item.getGoodsName());
        helper.setText(R.id.item_price, "￥" + item.getPrice());
        helper.setText(R.id.num, String.valueOf(item.getBuyCount()));


        helper.getView(R.id.jian).setTag(helper.getAdapterPosition());
        helper.getView(R.id.jia).setTag(helper.getAdapterPosition());
        helper.getView(R.id.del).setTag(helper.getAdapterPosition());
        helper.getView(R.id.check_item).setTag(helper.getAdapterPosition());
        helper.getView(R.id.jian).setOnClickListener(this::onClick);
        helper.getView(R.id.jia).setOnClickListener(this::onClick);
        helper.getView(R.id.del).setOnClickListener(this::onClick);
        helper.getView(R.id.check_item).setOnClickListener(this::onClick);

        CheckBox checkBox = helper.getView(R.id.check_item);
        if (item.getIsChecked()) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

    }


    @Override
    public void onClick(View v) {
        DaoManager daoManager = DaoManager.getInstance();
        DaoSession daoSession = daoManager.getDaoSession();

        int position = (int) v.getTag();
        BuyCarBean buyCarBean = getData().get(position);

        switch (v.getId()) {
            case R.id.jia:
                int buyCount = buyCarBean.getBuyCount();
                buyCarBean.setBuyCount(++buyCount);
                daoSession.update(buyCarBean);
                break;
            case R.id.jian:
                int buyCount1 = buyCarBean.getBuyCount();
                if (buyCount1 > 0) {
                    buyCarBean.setBuyCount(--buyCount1);
                }
                daoSession.update(buyCarBean);

                break;
            case R.id.del:
                getData().remove(buyCarBean);
                daoSession.delete(buyCarBean);
                break;
            case R.id.check_item:
                if (buyCarBean.getIsChecked()) {
                    buyCarBean.setIsChecked(false);
                } else {
                    buyCarBean.setIsChecked(true);
                }
                daoSession.update(buyCarBean);
                break;
        }
        getSumPrice();
        notifyDataSetChanged();
    }

    public float getSumPrice() {
        float sum =0;
        List<BuyCarBean> data = getData();
        for (BuyCarBean datum : data) {
            if (datum.getIsChecked()){
                 sum+= Float.parseFloat(datum.getPrice())*datum.getBuyCount();
            }
        }
        onAumentListener.onAument(Float.parseFloat(String.format("%.2f",sum)));
        return sum;
    }

    public interface OnAumentListener {
        void onAument(float sumPrice);
    }


}
